//
//  PersonalAlbumViewController.m
//  Findif
//
//  Created by Mac on 2019/3/27.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "PersonalAlbumViewController.h"
#import "UserAblumCollectionViewCell.h"
#import "ZJImageMagnification.h"

@interface PersonalAlbumViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView * collectionView;
@property (nonatomic, strong)NSArray * array;
@property (nonatomic, weak)UIImageView * spaceImageView;
@property (nonatomic, weak)UILabel * spaceLabel;

@end

@implementation PersonalAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ZRWhiteColor;
    self.title = @"他的相册";
    [self loadData];
    [self setupUI];
}


- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getUserPhotoAlbumWithUser_id:self.ID ResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.array = responseData[@"data"];
        }
    }];
}


- (void)setArray:(NSArray *)array{
    
    _array = array;
    
    [self.collectionView reloadData];
}


- (void)setupUI{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //设置CollectionView的属性
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 0, 345 * SCREENH6, kScreenH) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = ZRWhiteColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = YES;
    [self.view addSubview:self.collectionView];
    //注册Cell
    [self.collectionView registerClass:[UserAblumCollectionViewCell class] forCellWithReuseIdentifier:@"PersonalUserAblumCollectionViewCell"];
    
    UIImageView * spaceImageView = [UIImageView new];

    spaceImageView.image = [UIImage imageNamed:@"状态为空"];
    spaceImageView.hidden = YES;

    self.spaceImageView = spaceImageView;

    [self.collectionView addSubview:self.spaceImageView];


    UILabel * spaceLabel = [UILabel new];

    spaceLabel.text = @"对方尚未上传相片";
    spaceLabel.textColor = [UIColor lightGrayColor];
    spaceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    spaceLabel.textAlignment = NSTextAlignmentCenter;
    spaceLabel.hidden = YES;

    self.spaceLabel = spaceLabel;

    [self.collectionView addSubview:self.spaceLabel];
}

- (void)viewDidLayoutSubviews{
    
    [self.spaceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
    }];
    
    [self.spaceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.spaceImageView);
        make.top.mas_equalTo(self.spaceImageView.mas_bottom).offset(10 * SCREENH6);
    }];
}

#pragma mark  设置CollectionView的组数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark  设置CollectionView每组所包含的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.array.count == 0) {
        
        self.spaceImageView.hidden = NO;
        self.spaceLabel.hidden = NO;
    }else{
        
        self.spaceImageView.hidden = YES;
        self.spaceLabel.hidden = YES;
    }
    
    return self.array.count;
}

#pragma mark  设置CollectionCell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"PersonalUserAblumCollectionViewCell";
    UserAblumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    if (self.array.count > 0) {
        
        NSDictionary * dict = self.array[indexPath.item];
        
        NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"img"]];
        NSURL * url = [NSURL URLWithString:imgStr];
        [cell.imageView sd_setImageWithURL:url];
    }
    return cell;
}

#pragma mark  定义每个UICollectionView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(110 * SCREENH6,80 * SCREENH6);
}



#pragma mark  定义整个CollectionViewCell与整个View的间距
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UserAblumCollectionViewCell * cell = (UserAblumCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    [ZJImageMagnification scanBigImageWithImageView:cell.imageView alpha:1];
}

#pragma mark  定义每个UICollectionView的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5 * SCREENH6;
}

#pragma mark  定义每个UICollectionView的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5 * SCREENH6;
}


#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}


@end
